import localForage from "localforage";

// 本地收藏商家存储
export function setRecordSellers<T extends string, U = unknown>(key: T, val: U) {
    localForage.getItem<U[]>(key, function (err, value) {
        if (err) {
            console.log(err.stack);
        }
        const recordSeller: U[] = value || [];
        if (value === null) {
            console.warn('localForage：对应key并未存储实际值。');
        }
        recordSeller.push(val);
        localForage.setItem(key, recordSeller);
    });
}
// 本地收藏商家移除读取
export function removeRecordSellers<T extends string, U = unknown>(key: T, val: U) {
    localForage.getItem<U[]>(key, function (err, value) {
        if (err) {
            console.log(err.stack);
        }
        const recordSeller: U[] = value || [];
        if (value === null) {
            console.warn('localForage：对应key并未存储实际值。');
        }
        const rmIndex = recordSeller.indexOf(val);
        recordSeller.splice(rmIndex, 1);
        localForage.setItem(key, recordSeller);
    });
}
// 本地收藏商家读取
export function readRecordSellers<T extends string, U>(key: T, callFn: (value: U) => void) {
    localForage.getItem<U>(key, (err, value) => {
        if (err) {
            console.log(err.stack);
        }
        if (value != null) {
            callFn(value);
        }
    })
}
